package com.zpl.order.controller;


import com.alibaba.fastjson.JSON;
import com.zpl.core.Result;
import com.zpl.order.entity.Address;
import com.zpl.order.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/address")
@Api(value = "AddressController", tags = {"地址管理"})
public class AddressController {

    @Resource
    private AddressService addressService;


    @PostMapping
    @ApiOperation(value = "1、添加地址", notes = "添加地址", httpMethod = "POST")
    @ApiImplicitParam(name = "address", value = "地址信息", dataType = "Address",required = true)
    public Result insert(@RequestBody Address address) {

//        redisCacheTemplate.opsForValue().set("user:", user);
//        User user1 = (User) redisCacheTemplate.opsForValue().get("user:");


        if (addressService.insert(address)) {
            return Result.success("地址添加成功");
        }
        return Result.fail("系统错误");
    }

    @ApiOperation(value = "2、删除地址", notes = "按id删除地址", httpMethod = "DELETE")
    @ApiImplicitParam(name = "id", value = "id", dataType = "int",required = true)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        if (addressService.delete(id)) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    @ApiOperation(value = "6、查询地址", notes = "按id查询地址", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", dataType = "int",required = true)
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") Long id) {
        if (addressService.selectById(id)!=null) {
            return Result.success(addressService.selectById(id));
        }
        return Result.fail("查询失败");
    }

    @ApiOperation(value = "3、更改地址", notes = "更改地址", httpMethod = "PUT")
    @ApiImplicitParam(name = "address", value = "地址信息", dataType = "Address",required = true)
    @PutMapping
    public Result update(@RequestBody Address address) {
        if (addressService.update(address)) {
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }

    @ApiOperation(value = "4、获取地址列表", notes = "按id获取列表", httpMethod = "GET")
//    @ApiImplicitParam(name = "address", value = "地址信息", dataType = "Address",required = true)
    @GetMapping("/list")
    public Result getAddressByMemberId(@RequestParam("memberId") Long memberId) {
//        Long memberId = 1308242661102137346L;//等其他模块完成后再加
        List<Address> list = addressService.selectByMemberId(memberId);
        System.out.println(JSON.toJSON(list));
        return Result.success(list);
    }

    @ApiOperation(value = "5、查询默认地址", notes = "按id查询默认地址", httpMethod = "GET")
//    @ApiImplicitParam(name = "address", value = "地址信息", dataType = "Address",required = true)
    @GetMapping("/defaultAddress")
    public Result getDefaultAddress(@RequestParam("memberId") Long memberId) {
//        Long memberId = 1308242661102137346L;//等其他模块完成后再加
        Address address = addressService.selectByDefaultAddress(memberId);
        return Result.success(address);
    }


}
